package com.yz.jdd.webview;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;

import com.yz.jdd.base.MyApplication;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.Constants;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.webview.download.H5DownFileUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * webview 路由类
 */
public class Router {


    /**
     * 获取公共参数
     *
     * @return
     */
    public static String getCommonParam() {

        String server_url = PreferencesUtils.getString(MyApplication.application, BaseConfigs.STRING_SERVER_URL, Constants.SERVER_URL);
        String sessinoId = PreferencesUtils.getString(MyApplication.application, BaseConfigs.PREF_SESSIONID);
        return "?s=" + sessinoId + "&statusBarHeight=0&android=4.1" + "&SDK=" + Build.VERSION.SDK_INT + "&url=" + server_url + "&close=true";
    }

    /**
     * 打开webview 传入路由地址
     *
     * @param context
     * @param url
     */
    public static void startWebView(Context context, String url) {
        Intent intent = new Intent(context, H5BridgeActivity.class);
        String main_url = "file://" + H5DownFileUtils._UPZIP_FOLDERPATH + url + getCommonParam();
        intent.putExtra(Constants.H5_WEB_URL, main_url);
        String sessionId=PreferencesUtils.getString(context, BaseConfigs.PREF_SESSIONID);
        intent.putExtra("sessionId",sessionId);
        context.startActivity(intent);
    }

    /**
     * 打开webview 传入路由地址
     *
     * @param context
     * @param url
     */
    public static void startWebView(Context context, String url,int flag) {
        Intent intent = new Intent(context, H5BridgeActivity.class);
        String main_url = "file://" + H5DownFileUtils._UPZIP_FOLDERPATH + url + getCommonParam();
        intent.putExtra(Constants.H5_WEB_URL, main_url);
        intent.setFlags(flag);
        String sessionId=PreferencesUtils.getString(context, BaseConfigs.PREF_SESSIONID);
        intent.putExtra("sessionId",sessionId);
        context.startActivity(intent);
    }

    /**
     * 打开webview 传入路由地址
     *
     * @param context
     * @param url
     */
    public static void startWebView(Context context, String url, boolean isFromServer) {
        Intent intent = new Intent(context, H5BridgeActivity.class);
        String sessionId=PreferencesUtils.getString(context, BaseConfigs.PREF_SESSIONID);
        intent.putExtra("sessionId",sessionId);
        String startWithfile = "file://" + H5DownFileUtils._UPZIP_FOLDERPATH;
        String main_url = url + getCommonParam();

        if (!isFromServer) {
            main_url = startWithfile + main_url;
        }

        intent.putExtra(Constants.H5_WEB_URL, main_url);
        context.startActivity(intent);
    }

    /**
     * 在新页面里打开webview
     *
     * @param context
     * @param url
     * @param title
     * @param isFromServer
     */
    public static void startNewWebView(Context context, String url,String backUrl, String title, boolean isFromServer) {
        Intent intent = new Intent(context, H5BridgeOtherActivity.class);
        String sessionId=PreferencesUtils.getString(context, BaseConfigs.PREF_SESSIONID);
        intent.putExtra("sessionId",sessionId);
        String startWithfile = "file://" + H5DownFileUtils._UPZIP_FOLDERPATH;
        String main_url = url + getCommonParam();

        if (!isFromServer) {
            main_url = startWithfile + main_url;
        }

        intent.putExtra(Constants.H5_WEB_URL, main_url);
        if(!TextUtils.isEmpty(title)){
            intent.putExtra("title", title);
        }

        if(!TextUtils.isEmpty(backUrl)){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("backUrl", backUrl);
        }
        context.startActivity(intent);
    }


    /**
     * 对账单
     */
    public static final String accountStatement = "vuepages/index.html#/accountStatement";

    /**
     * 经营分析
     */
    public static final String businessAnalysis = "vuepages/index.html#/businessAnalysis";

    /**
     * 商城
     */
    public static final String mallList = "vuepages/index.html#/mallList";

    /**
     * 跳转扫一扫
     */
    public static void startScanning(Context context, String result) {
        Intent intent = new Intent(context, H5BridgeActivity.class);
        String sessionId=PreferencesUtils.getString(context, BaseConfigs.PREF_SESSIONID);
        intent.putExtra("sessionId",sessionId);
        String code = "";
        try {
            code = URLEncoder.encode(result, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String url = "vuepages/index.html#/scanResult";
        String main_url = "file://" + H5DownFileUtils._UPZIP_FOLDERPATH + url + getCommonParam() + "&scanResult=" + code;
        LogUtils.e("=== mainUrl: "+main_url);
        intent.putExtra(Constants.H5_WEB_URL, main_url);
        context.startActivity(intent);
    }

    /**
     * 跳转详情h5页面
     * @param context
     * @param orderId 订单id
     * @param orderType 订单类型
     * @param shopId 门店id
     * @param orderStatus 订单状态
     * @param detailType 区分流水还是订单：1:订单 ,2:流水
     * @param tradeType 流水的类型
     */
    public static void startOrderDetail(Context context, String orderId, String orderType, String shopId, String orderStatus,String detailType ,String tradeType) {
        Intent intent = new Intent(context, H5BridgeActivity.class);
        String sessionId=PreferencesUtils.getString(context, BaseConfigs.PREF_SESSIONID);
        intent.putExtra("sessionId",sessionId);
        //url="vuepages/index.html#"
        String url = "vuepages/index.html#/orderDetails?orderId=" + orderId + "&orderType=" + orderType + "&shopId=" + shopId + "&orderStatus=" + orderStatus;
        String server_url = PreferencesUtils.getString(MyApplication.application, BaseConfigs.STRING_SERVER_URL, Constants.SERVER_URL);
        String main_url = "file://" + H5DownFileUtils._UPZIP_FOLDERPATH + url + "&s=" +
                sessionId +
                "&statusBarHeight=0&android=4.1" + "&SDK=" + Build.VERSION.SDK_INT + "&url=" + server_url + "&close=true"
                +"&detailType="+detailType +"&tradeType="+tradeType;
        intent.putExtra(Constants.H5_WEB_URL, main_url);


        context.startActivity(intent);
    }

    /**
     * 跳转个人信息详情
     */
    public static void startPersonInfo(Context context, String affiliationName, String code, String mobile, String name, String roleName) {
        Intent intent = new Intent(context, H5BridgeActivity.class);
        String sessionId=PreferencesUtils.getString(context, BaseConfigs.PREF_SESSIONID);
        intent.putExtra("sessionId",sessionId);
        //url="vuepages/index.html#"
        String url = "vuepages/index.html#/myDetail?affiliationName=" + affiliationName + "&code=" + code + "&mobile=" + mobile + "&name=" + name + "&roleName=" + roleName;
        String server_url = PreferencesUtils.getString(MyApplication.application, BaseConfigs.STRING_SERVER_URL, Constants.SERVER_URL);
        String main_url = "file://" + H5DownFileUtils._UPZIP_FOLDERPATH + url + "&s=" +
                sessionId +
                "&statusBarHeight=0&android=4.1" + "&SDK=" + Build.VERSION.SDK_INT + "&url=" + server_url + "&close=true";
        intent.putExtra(Constants.H5_WEB_URL, main_url);


        context.startActivity(intent);
    }

    /**
     *
     * @param context
     * @param shopId
     * @param shopName
     * @param ticketType  电子券类型 手动：0或者是扫码：1
     * @param ticketCode
     */
    public static void startCancelVerifi(Context context, String shopId, String shopName, String ticketType, String ticketCode){
        Intent intent = new Intent(context, H5BridgeActivity.class);
        String sessionId=PreferencesUtils.getString(context, BaseConfigs.PREF_SESSIONID);
        intent.putExtra("sessionId",sessionId);

        String url = cancelVerifi+"?shopId=" + shopId + "&shopName=" + shopName + "&ticketType=" + ticketType + "&ticketCode=" + ticketCode;
        String server_url = PreferencesUtils.getString(MyApplication.application, BaseConfigs.STRING_SERVER_URL, Constants.SERVER_URL);
        String main_url = "file://" + H5DownFileUtils._UPZIP_FOLDERPATH + url + "&s=" +
                sessionId +
                "&statusBarHeight=0&android=4.1" + "&SDK=" + Build.VERSION.SDK_INT + "&url=" + server_url + "&close=true";

        intent.putExtra(Constants.H5_WEB_URL, main_url);


        context.startActivity(intent);
    }
    /**
     * 首页
     */
    public static final String home_url = "vuepages/index.html#/home";
    /**
     * 数字化
     */
    public static final String digitization_url = "vuepages/index.html#/businessAnalysis";

    /**
     * 我的
     */
    public static final String my_url = "vuepages/index.html#/my";

    /**
     * 金融
     */
    public static final String finance_url = "vuepages/index.html#/finance";
    /**
     * 共享付
     */
    public static final String finance_child_url = "vuepages/index.html#/financeChild";
    /**
     * 同福贷
     */
    public static final String tongfudai_url = "vuepages/index.html#/tongfuLoan";
    /**
     * 个人信息
     */
    public static final String my_detail = "vuepages/index.html#/myDetail";

    /**
     * 门店管理
     */
    public static final String storeManage = "vuepages/index.html#/storeManage";

    /**
     * 员工管理
     */
    public static final String employee_manage = "vuepages/index.html#/employeeManage";

    /**
     * 机具管理
     */
    public static final String machinesManage = "vuepages/index.html#/machinesManage";

    /**
     * 语音播报
     */
    public static final String voice = "vuepages/index.html#/voice";

    /**
     * 设置
     */
    public static final String mine_setting = "vuepages/index.html#/setting";

    /**
     * 首页消息
     */
    public static final String newsJson = "vuepages/static/routerConfig.json";

    /**
     * 调试页
     */
    public static final String testPage = "vuepages/index.html#/testPage";

//    /**
//     * 待办事项
//     */
//    public static final String backlogPage = "vuepages/index.html#/todoList";

    /**
     * 常见问题
     */
    public static final String questionPage = "http://bmserver.zonghengke.com/bmwh5/index.html#/commonProblemJD";
    /**
     * 小程序
     */
    public static final String miniAppManage = "vuepages/index.html#/miniAppManage";
    /**
     * 会员管理
     */
    public static final String memberManage = "vuepages/index.html#/memberManage";


    /**
     * 联系人BD
     */
    public static final String  concat="vuepages/index.html#/concat";


    /**
     * 核销券的页面地址
     */
    public static final String  cancelVerifi="vuepages/index.html#/cancelVerifi";
    /**
     * 我的贷款
     */
    public static final String  myLoan="vuepages/index.html#/myLoan";

}
